package com.caigou.modules.forum.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.caigou.comm.base.bean.CommentBean
import com.caigou.comm.base.bean.CommentItemBean
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.forum.bean.PostDetailBean
import com.caigou.modules.forum.bean.PacketRecordBean
import com.caigou.modules.forum.repository.ForumRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import java.io.File
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/12/18
 */
@HiltViewModel
class PostViewModel @Inject constructor(private val repository: ForumRepository) : ViewModel() {

    val postDetailLiveData = MutableLiveData<Resource<PostDetailBean>>()
    val commentListLiveData = MutableLiveData<Resource<CommentBean>>()
    val commentDetailLiveData = MutableLiveData<Resource<CommentItemBean>>()
    val postReplyLiveData = MutableLiveData<Resource<String>>()
    val commentReplyLiveData = MutableLiveData<Resource<String>>()

    fun getPostDetail(dataId: Int, currentUserId: String) {
        postDetailLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getPostDetail(dataId, currentUserId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<PostDetailBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    postDetailLiveData.value = it
                }
        }
    }

    fun getCommentList(page: Int, limit: Int, dataId: Int, sort: Int, currentUserId: String) {
        commentListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getCommentList(page, limit, dataId, sort, currentUserId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<CommentBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    commentListLiveData.value = it
                }
        }
    }

    fun getCommentDetail(dataId: Int, replyId: Int, userId: String) {
        commentDetailLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getCommentDetail(dataId, replyId, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<CommentItemBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    commentDetailLiveData.value = it
                }
        }
    }

    fun setPostReply(content: String, files: MutableList<File>, senderId: String, receiverId: String, dataId: Int) {
        postReplyLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.setPostReply(content, files, senderId, receiverId, dataId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.msg)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    postReplyLiveData.value = it
                }
        }
    }

    fun setCommentReply(content: String, files: MutableList<File>, senderId: String, receiverId: String, dataId: Int, parentId: Int) {
        commentReplyLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.setCommentReply(content, files, senderId, receiverId, dataId, parentId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.msg)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    commentReplyLiveData.value = it
                }
        }
    }
}